package cn.iocoder.yudao.module.promotion.controller.admin.points;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.points.vo.pointsactivity.*;
import cn.iocoder.yudao.module.promotion.convert.points.PointsActivityConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.points.PointsActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.points.PointsProductDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillActivityDO;
import cn.iocoder.yudao.module.promotion.service.points.PointsActivityService;
import cn.iocoder.yudao.module.promotion.service.seckill.SeckillActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.hutool.core.collection.CollUtil.newArrayList;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 积分活动")
@RestController
@RequestMapping("/promotion/points-activity")
@Validated
public class PointsActivityController {

    @Resource
    private PointsActivityService pointsActivityService;

    @Resource
    private SeckillActivityService seckillActivityService;

    @PostMapping("/create")
    @Operation(summary = "创建积分活动")
    @PreAuthorize("@ss.hasPermission('promotion:points-activity:create')")
    public CommonResult<Long> createPointsActivity(@Valid @RequestBody PointsActivityCreateReqVO createReqVO) {
        return success(pointsActivityService.createPointsActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新积分活动")
    @PreAuthorize("@ss.hasPermission('promotion:points-activity:update')")
    public CommonResult<Boolean> updatePointsActivity(@Valid @RequestBody PointsActivityUpdateReqVO updateReqVO) {
        pointsActivityService.updatePointsActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除积分活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:points-activity:delete')")
    public CommonResult<Boolean> deletePointsActivity(@RequestParam("id") Long id) {
        pointsActivityService.deletePointsActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得积分活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:points-activity:query')")
    public CommonResult<PointsActivityRespVO> getPointsActivity(@RequestParam("id") Long id) {
        PointsActivityDO pointsActivity = pointsActivityService.getPointsActivity(id);
        List<PointsProductDO> productsList = pointsActivityService.getPointsProductListByActivityIds(newArrayList(id));
        return success(PointsActivityConvert.INSTANCE.convert(pointsActivity,productsList));
    }

    @GetMapping("/list")
    @Operation(summary = "获得积分活动列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('promotion:points-activity:query')")
    public CommonResult<List<PointsActivityRespVO>> getPointsActivityList(@RequestParam("ids") Collection<Long> ids) {
        List<PointsActivityDO> list = pointsActivityService.getPointsActivityList(ids);
        return success(PointsActivityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得积分活动分页")
    @PreAuthorize("@ss.hasPermission('promotion:points-activity:query')")
    public CommonResult<PageResult<PointsActivityRespVO>> getPointsActivityPage(@Valid PointsActivityPageReqVO pageVO) {
        PageResult<PointsActivityDO> pageResult = pointsActivityService.getPointsActivityPage(pageVO);
        return success(PointsActivityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出积分活动 Excel")
    @PreAuthorize("@ss.hasPermission('promotion:points-activity:export')")
    //@OperateLog(type = EXPORT)
    public void exportPointsActivityExcel(@Valid PointsActivityExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PointsActivityDO> list = pointsActivityService.getPointsActivityList(exportReqVO);
        // 导出 Excel
        List<PointsActivityExcelVO> datas = PointsActivityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "积分活动.xls", "数据", PointsActivityExcelVO.class, datas);
    }

    @GetMapping("/pointsList")
    @Operation(summary = "获得所有活动分页")
    @PreAuthorize("@ss.hasPermission('promotion:points-activity:query')")
    public CommonResult<List<ActivityListVO>> pointsList(@Valid PointsActivityPageReqVO pageVO) {
        List<PointsActivityDO> pointsList = pointsActivityService.pointsList(pageVO);
        List<ActivityListVO> al = PointsActivityConvert.INSTANCE.convertList03(pointsList);
        List<SeckillActivityDO> seckillList = seckillActivityService.getSeckillActivityList();
        List<ActivityListVO> sl = PointsActivityConvert.INSTANCE.convertList04(seckillList);
        al.addAll(sl);
        return success(al);
    }

}
